package com.lrs.java.j2se_advanced.regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class Test {
	public static void main(String[] args) {
//		System.out.println("abcd".matches("..."));
//		f("123".matches("\\d{3}"));
//		f("a23sf af342 ".replaceAll("\\s", "-"));
//		Pattern p = Pattern.compile("[a-z]{3}");
//		Matcher m = p.matcher("abc");
//		f(m.matches());
//		f("abc".matches("[a-z]{3}"));
		
		
		//认识 . * + ?  []  ^  {}
		
//		f("".matches("a?")); // one or zero of a;
//		f("aaaa".matches("aaaaa+")); // one or more of a
//		f("aaaa".matches("aaaaa*"));  // zero or more of a
//		f("aaaa".matches("aaa."));
//		f("aaaa".matches("aaaa."));
//		f("aaaa".matches("[a-z]{3,100}"));
//		f("aaaa".matches("[^\\d]{1,5}"));
//		f("3415153135525".matches("\\d{10,}"));
//		f("127.0.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
//		f("127".matches("[0-2][0-9][0-9]"));
//		f("243+434+3r3".replaceAll("\\+", "-"));
		
		// Poxix 
//		f("abcd".matches("\\p{Lower}{4}"));
		
		// Boundary
		
//		f("	".matches("\\b"));
//		f("hello java".matches("^h[a-z]{1,3}o\\b\\sjava$"));
//		f("hellojava".matches("^h[a-z]{1,3}o\\b.*"));
//		f("hello java".matches(".*va$"));
//		f("hello a".matches("^h[a-z]{1,3}o\\b.*"));
//		f("hello java".matches("^h[a-z]{1,3}o\\b.*java$"));
//		f(" \n".matches("^[\\s&&[^\\n]]*\\n$")); //空白行
		
		
		// example  IP AND Email
//		f("192.168.0.1".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
//		f("zhanglei1125@avl.com.cn".matches("[\\w[.-]]+@[\\w]+\\.[\\w]+"));

		//matches() find() lookingAt() reset()
		/*
		Pattern p = Pattern.compile("\\d{3,5}");
		String str = "213-34355-3124-00";
		Matcher m = p.matcher(str);
		
		f(m.matches());
		m.reset();
		
		f(m.find(2));
		f(m.start() + "-" + m.end());
		f(m.find(10));
		f(m.start() + "-" + m.end());
		f(m.find());
		//f(m.start() + "-" + m.end()); // 如果不能够找到，则用start() 与 end() 出错
		f(m.find());
		
		f(m.lookingAt());
		f(m.lookingAt());
		f(m.lookingAt());
		*/
		// 运用Macher类的find()来查找匹配的字符串  replacement
		
		Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
		String str = " java Java JAva JAVa I'm love JAVA java fdasfd";
		Matcher m = p.matcher(str);
		//f(m.replaceAll("java"));
		
		// 运用String类的find()来查找匹配的字符串  replacement
		//str = str.replaceAll("[jJ][aA][vV][aA]", "java");
		//f(str);
		
		
		StringBuffer sb = new StringBuffer();
		int i = 0;
		while(m.find()) {
			i++;
			if(i%2 == 0) {
				m.appendReplacement(sb, "java");
			} else {
				m.appendReplacement(sb, "JAVA");
			}
			f(m.group());
		}
		m.appendTail(sb);
		f(sb);
		
		
	}
	
	public static void f(Object o) {
		System.out.println(o);
	}
}	